package com.rfs.interceptor;

import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.interceptor.Interceptor;
import com.dtflys.forest.utils.ForestDataType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * forest拦截器
 */
@Slf4j
@Component
public class ForestInterceptor implements Interceptor {

    /**
     * 该方法在请求发送之前被调用, 若返回false则不会继续发送请求
     */
    @Override
    public boolean beforeExecute(ForestRequest request) {
        String uri = request.getUrl().replace("http://", "");
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>uri：{}", uri);
        request.setDataType(ForestDataType.JSON);
        request.setContentType("application/json");
        // 添加请求参数access_token
       /* String token = "";
        request.addQuery("access_token", token);
        //添加服务器统一请求地址
        request.setUrl("https://www.baidu.com/"+ uri);*/
        request.setUrl("https://www.baidu.com/");
        return true;
    }


    /**
     * 请求发送失败时被调用
     *
     * @param e
     * @param forestRequest
     * @param forestResponse
     */
    @Override
    public void onError(ForestRuntimeException e, ForestRequest forestRequest, ForestResponse forestResponse) {
        throw new RuntimeException(forestResponse.getContent());
    }

    /**
     * 请求成功调用(微信端错误异常统一处理)
     *
     * @param o
     * @param forestRequest
     * @param forestResponse
     */
    @Override
    public void onSuccess(Object o, ForestRequest forestRequest, ForestResponse forestResponse) {
        log.info("url:[{}],result:[{}]", forestRequest.getUrl(), forestResponse.getContent());

    }


}
